package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.Dao;

import modelo.Usuario;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
	
	private final String loginUsuario = "wandersonmello_17_18@hotmail.com";
	private final String senhaUsuario = "123456";
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.invalidate();
		response.sendRedirect("topo_inscrever.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");
		
		
		
		Usuario usuario = (Usuario) Dao.getInstance().criarQuery("FROM usuario WHERE email = '"+login+"' AND senha = '"+senha+"'");
		//usuario.setLogin(login);
		//usuario.setSenha(senha);
		
		RequestDispatcher rd = null;
		if(usuario!=null){
			HttpSession session = request.getSession();
			session.setAttribute("usuario_autenticado", usuario);
			rd = request.getRequestDispatcher("usuariologout.jsp");
		}else{
			HttpSession session = request.getSession();
			session.setAttribute("usuario_autenticado", usuario);
			rd = request.getRequestDispatcher("index.jsp?msg=erro");
		}
		rd.forward(request, response);
	}

}
